//
//  HttpRequester.m
//  leo
//
//  Created by 常艳 on 15/8/25.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "HttpRequester.h"
#import "AppDelegate.h"

@implementation HttpRequester
-(id)init
{
    self=[super init];
    self.theManager=[[DataManager alloc] init];
    static MKNetworkEngine *engineInstance=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSMutableDictionary *header=[[NSMutableDictionary alloc] init];
        [header setValue:@"ios" forKey:@"User-Agent"];
        engineInstance = [[MKNetworkEngine alloc] initWithHostName:hostName customHeaderFields:header];;
    });
    self.engine=engineInstance;
    return self;
}
-(NSDictionary *)getUrl:(NSString *)url
{
    NSLog(@"%s,getUrl entered, the url is %@",http_tag,url);
    if(url==NULL||[url length]<=0)
    {
        NSLog(@"%s,getUrl,url is null",http_tag);
        return NULL;
    }
    NSURL *geturl=[NSURL URLWithString:url];
    if(geturl==NULL)
    {
        NSLog(@"%s,getUrl,string to url failed",http_tag);
        return NULL;
    }
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:geturl];
    [request setValue:@"ios" forHTTPHeaderField:@"User-Agent"];
    NSData *data=[NSURLConnection  sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data==NULL)
    {
        NSLog(@"%s,getUrl,data is null",http_tag);
        return NULL;
    }
    NSError *jserror=NULL;
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jserror];
    if(dict==NULL)
    {
        NSLog(@"%s,getUrl,convert to json object fail",http_tag);
        return NULL;
    }
    NSLog(@"%s,getUrl,length is %lu",http_tag,(unsigned long)[dict count]);
    return dict;
}

-(NSDictionary *)postUrl:(NSString *)url postdata:(NSString *)parameters
{
    NSLog(@"%s,postUrl entered, the url is %@",http_tag,url);
    if(url==NULL||[url length]<=0)
    {
        NSLog(@"%s,postUrl,url is null",http_tag);
        return NULL;
    }
    NSURL *posturl=[NSURL URLWithString:url];
    if(posturl==NULL)
    {
        NSLog(@"%s,postUrl,string to url failed",http_tag);
        return NULL;
    }
    NSData *postData=[parameters dataUsingEncoding:NSUTF8StringEncoding];
    if(postData==NULL)
    {
        NSLog(@"%s,postUrl,postdata to url failed",http_tag);
        return NULL;
    }
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:posturl];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setValue:@"ios" forHTTPHeaderField:@"User-Agent"];
    NSData *data=[NSURLConnection  sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jserror=NULL;
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jserror];
    if(dict==NULL)
    {
        NSLog(@"%s,getUrl,convert to json object fail",http_tag);
        return NULL;
    }
    NSLog(@"%s,getUrl,length is %lu",http_tag,(unsigned long)dict.count);
    return dict;
}
-(void)asyncGet:(NSString *)url parameters:(NSString *)parameters completion:(void (^)(MKNetworkOperation *operation))callback
{
    NSLog(@"%s,asyncGet entered, the url is %@",http_tag,url);
    if(url==NULL||[url length]<=0)
    {
        NSLog(@"%s,asyncGet,url is null",http_tag);
        return ;
    }
    if(callback==NULL)
    {
        NSLog(@"%s,asyncGet,callback is null",http_tag);
        return;
    }
    NSURL *asyncUrl=[NSURL URLWithString:url];
    if(asyncUrl==NULL)
    {
        NSLog(@"%s,asyncGet,string to url failed",http_tag);
        return ;
    }
    NSString *host=[asyncUrl host];
    NSString *path=[asyncUrl path];
    NSString *asyncPath=[path stringByAppendingString:parameters];
    if(host==NULL||[host length]<=0)
    {
        NSLog(@"%s,asyncGet,host is null",http_tag);
        return ;
    }

    @try {
        NSMutableDictionary *header=[[NSMutableDictionary alloc] init];
        [header setValue:@"ios" forKey:@"User-Agent"];
        //MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:host customHeaderFields:header];
        if(self.engine==NULL)
        {
            NSLog(@"%s,asyncGet,engine is null",http_tag);
            return;
        }
        MKNetworkOperation *op=[self.engine operationWithPath:asyncPath];
        if(op==NULL)
        {
            NSLog(@"%s,asyncGet,op is null",http_tag);
            return;
        }
        [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
            callback(completedOperation);
        } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
            NSLog(@"%@",[error localizedDescription]);
        }];
        [self.engine enqueueOperation:op];
        
    }
    @catch (NSException *ex) {
        NSLog(@"%s,asyncGet,exception is %@",http_tag,ex.reason);
    }
    @finally {
        
    }

    
    
}

-(void)asyncPost:(NSString *)url parameters:(NSDictionary *)parameters completion:(void (^)(NSDictionary *resdict))callback
{
    NSLog(@"%s,asyncPost entered, the url is %@",http_tag,url);
    if(url==NULL||[url length]<=0)
    {
        NSLog(@"%s,asyncPost,url is null",http_tag);
        return ;
    }
    if(callback==NULL)
    {
        NSLog(@"%s,asyncPost,callback is null",http_tag);
        return;
    }
    NSURL *asyncUrl=[NSURL URLWithString:url];
    if(asyncUrl==NULL)
    {
        NSLog(@"%s,asyncPost,string to url failed",http_tag);
        return ;
    }
    NSString *hostStr=[asyncUrl host];
    NSString *path=[asyncUrl path];
    NSNumber *port=[asyncUrl port];
    NSString *host=NULL;
    if(port>0)
        host=[NSString stringWithFormat:@"%@:%d",hostStr,[port intValue]];
    else
        host=hostStr;
    if(host==NULL||[host length]<=0)
    {
        NSLog(@"%s,asyncPost,host is null",http_tag);
        return ;
    }
    @try {
        NSMutableDictionary *header=[[NSMutableDictionary alloc] init];
        [header setValue:@"ios" forKey:@"User-Agent"];
        //MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:host customHeaderFields:header];
        if(self.engine==NULL)
        {
            NSLog(@"%s,asyncPost,engine is null",http_tag);
            return;
        }
        MKNetworkOperation *op=[self.engine operationWithPath:path params:parameters httpMethod:@"POST" ssl:YES];
        op.shouldContinueWithInvalidCertificate=YES;
        if(op==NULL)
        {
            NSLog(@"%s,asyncPost,op is null",http_tag);
            return;
        }
        [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
            if(completedOperation==NULL)
            {
                NSLog(@"%s,asyncPost,completedOperation is null",http_tag);
                callback(NULL);
                return ;
            }
            NSData *data=[completedOperation responseData];
            if(data==NULL)
            {
                NSLog(@"%s,asyncPost,data is null",http_tag);
                callback(NULL);
                return;
            }
            NSDictionary *resdict=NULL;
            @try {
                resdict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                if(resdict==NULL||[resdict count]==0)
                {
                    NSLog(@"%s,asyncPost,resdict is null",http_tag);
                    callback(NULL);
                    return;
                }
            }
            @catch (NSException *exception) {
                NSString *errorStr= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                [LeoMsg showMsg:[NSString stringWithFormat:@"json解析失败,errorStr is %@.",errorStr]];
                callback(NULL);
                return;
            }
            @finally {

            }

            NSString *errorCode=[resdict valueForKey:@"errorCode"];
            //if(errorCode!=NULL&&[errorCode integerValue]==DEVICE_ACCESS_TOKEN_EXPIRED)
            if(errorCode!=NULL&&[errorCode integerValue]==USER_ACCESS_TOKEN_EXPIRED)
            {
                NSLog(@"%s,asyncPost,access token expired,get new accessToken",http_tag);
                ReRequester *reRequester=[[ReRequester alloc] init];
                if(reRequester!=NULL)
                {
                    reRequester.url=url;
                    reRequester.postPara=parameters;
                    reRequester.waitingCallBack=callback;
                    reRequester.methord=ASYNCPOST;
                    [self fetchNewAccessToken:reRequester];
                    return;
                }

            }
            callback(resdict);
        } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
            NSLog(@"%s,asyncPost,error is %@",http_tag,[error localizedDescription]);
            callback(NULL);
        }];
        [self.engine enqueueOperation:op];
        
    }
    @catch (NSException *ex) {
        NSLog(@"%s,asyncPost,exception is %@",http_tag,ex.reason);
        callback(NULL);
    }
    @finally {
        
    }
    
    
    
}


-(void)asyncPostOther:(NSString *)url parameters:(NSDictionary *)parameters completion:(void (^)(NSDictionary *resdict))callback
{
    NSLog(@"%s,asyncPostOther entered, the url is %@",http_tag,url);
    if(url==NULL||[url length]<=0)
    {
        NSLog(@"%s,asyncPostOther,url is null",http_tag);
        return ;
    }
    if(callback==NULL)
    {
        NSLog(@"%s,asyncPostOther,callback is null",http_tag);
        return;
    }
    NSURL *asyncUrl=[NSURL URLWithString:url];
    if(asyncUrl==NULL)
    {
        NSLog(@"%s,asyncPostOther,string to url failed",http_tag);
        return ;
    }
    NSString *hostStr=[asyncUrl host];
    NSString *path=[asyncUrl path];
    NSNumber *port=[asyncUrl port];
    NSString *host=NULL;
    if(port>0)
        host=[NSString stringWithFormat:@"%@:%d",hostStr,[port intValue]];
    else
        host=hostStr;
    if(host==NULL||[host length]<=0)
    {
        NSLog(@"%s,asyncPostOther,host is null",http_tag);
        return ;
    }
    @try {
        NSMutableDictionary *header=[[NSMutableDictionary alloc] init];
        [header setValue:@"ios" forKey:@"User-Agent"];
        //MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:host customHeaderFields:header];
        if(self.engine==NULL)
        {
            NSLog(@"%s,asyncPostOther,engine is null",http_tag);
            return;
        }
        MKNetworkOperation *op=[self.engine operationWithPath:path params:parameters httpMethod:@"POST" ssl:YES];
        op.shouldContinueWithInvalidCertificate=YES;
        if(op==NULL)
        {
            NSLog(@"%s,asyncPostOther,op is null",http_tag);
            return;
        }
        [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
            if(completedOperation==NULL)
            {
                NSLog(@"%s,asyncPostOther,completedOperation is null",http_tag);
                callback(NULL);
                return ;
            }
            NSData *data=[completedOperation responseData];
            if(data==NULL)
            {
                NSLog(@"%s,asyncPostOther,data is null",http_tag);
                callback(NULL);
                return;
            }
            NSDictionary *resdict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            if(resdict==NULL||[resdict count]==0)
            {
                NSLog(@"%s,asyncPostOther,resdict is null",http_tag);
                callback(NULL);
                return;
            }
            NSString *errorCode=[resdict valueForKey:@"errorCode"];
            //if(errorCode!=NULL&&[errorCode integerValue]==DEVICE_ACCESS_TOKEN_EXPIRED)
            if(errorCode!=NULL&&[errorCode integerValue]==USER_ACCESS_TOKEN_EXPIRED)
            {
                NSLog(@"%s,asyncPost,access token expired,get new accessToken",http_tag);
                ReRequester *reRequester=[[ReRequester alloc] init];
                if(reRequester!=NULL)
                {
                    reRequester.url=url;
                    reRequester.postPara=parameters;
                    reRequester.waitingCallBack=callback;
                    reRequester.methord=ASYNCPOST;
                    [self fetchNewAccessToken:reRequester];
                    return;
                }
                
            }
            callback(resdict);
        } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
            NSLog(@"%s,asyncPost,error is %@",http_tag,[error localizedDescription]);
            callback(NULL);
        }];
        [self.engine enqueueOperation:op];
        
    }
    @catch (NSException *ex) {
        NSLog(@"%s,asyncPost,exception is %@",http_tag,ex.reason);
        callback(NULL);
    }
    @finally {
        
    }
    
    
    
}

-(void)postFile:(NSString *)urlStr   fileName:(NSString *)fileName filePath:(NSString *)filePath param:(NSDictionary *)param completion:(void (^)(NSDictionary *resdict))callback
{
    NSLog(@"%s,postFile entered, the urlStr is %@, fileName is %@, filePath is %@.",http_tag,urlStr,fileName,filePath);
    if(urlStr==NULL||[urlStr length]<=0)
    {
        NSLog(@"%s,postFile,urlStr is null",http_tag);
        callback(NULL);
        return ;
    }
    if(callback==NULL)
    {
        NSLog(@"%s,postFile,callback is null",http_tag);
        return;
    }
    NSURL *upurl=[NSURL URLWithString:urlStr];
    if(upurl==NULL)
    {
        NSLog(@"%s,postFile,string to url failed",http_tag);
        callback(NULL);
        return ;
    }
    
    NSString *hostStr=[upurl host];
    NSString *path=[upurl path];
    NSNumber *port=[upurl port];
    NSString *host=[NSString stringWithFormat:@"%@:%d",hostStr,[port intValue]];
    if(host==NULL||[host length]<=0)
    {
        NSLog(@"%s,postFile,host is null",http_tag);
        return ;
    }
    
    @try {
        NSString *boundary=@"******";

        NSString *strContentType=[NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
        NSMutableDictionary *header=[[NSMutableDictionary alloc] init];
        [header setValue:@"ios" forKey:@"User-Agent"];
        [header setValue:@"Keep-Alive" forKey:@"Connection"];
        [header setValue:@"UTF-8" forKey:@"Charset"];
        if(filePath!=NULL&&![filePath isEqualToString:@"(null)"])
        {
            [header setValue:strContentType forKey:@"Content-Type"];
        }
        
        //MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:host customHeaderFields:header];
        if(self.engine==NULL)
        {
            NSLog(@"%s,postFile,engine is null",http_tag);
            return;
        }
        MKNetworkOperation *op=[self.engine operationWithPath:path params:param httpMethod:@"POST" ssl:YES];
        op.shouldContinueWithInvalidCertificate=YES;
        if(op==NULL)
        {
            NSLog(@"%s,postFile,op is null",http_tag);
            return;
        }
        if(filePath!=NULL&&![filePath isEqualToString:@"(null)"])
        {
            [op addFile:filePath forKey:@"file"];
        }
        
        [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
            if(completedOperation==NULL)
            {
                NSLog(@"%s,postFile,completedOperation is null",http_tag);
                callback(NULL);
                return ;
            }
            NSData *data=[completedOperation responseData];
            if(data==NULL)
            {
                NSLog(@"%s,postFile,data is null",http_tag);
                callback(NULL);
                return;
            }
            NSDictionary *resdict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            if(resdict==NULL||[resdict count]==0)
            {
                NSLog(@"%s,postFile,resdict is null",http_tag);
                callback(NULL);
                return;
            }
            NSString *errorCode=[resdict valueForKey:@"errorCode"];
            //if(errorCode!=NULL&&[errorCode integerValue]==DEVICE_ACCESS_TOKEN_EXPIRED)
            if(errorCode!=NULL&&[errorCode integerValue]==USER_ACCESS_TOKEN_EXPIRED)
            {
                NSLog(@"%s,asyncPost,access token expired,get new accessToken",http_tag);
                ReRequester *reRequester=[[ReRequester alloc] init];
                if(reRequester!=NULL)
                {
                    reRequester.url=urlStr;
                    reRequester.postPara=param;
                    reRequester.waitingCallBack=callback;
                    reRequester.methord=POSTFILE;
                    reRequester.filePath=filePath;
                    reRequester.fileName=fileName;
                    [self fetchNewAccessToken:reRequester];
                    return;
                }
                
            }
            callback(resdict);
        } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
            NSLog(@"%s,postFile,error is %@",http_tag,[error localizedDescription]);
            callback(NULL);
        }];
        [self.engine enqueueOperation:op];
        
    }
    @catch (NSException *ex) {
        NSLog(@"%s,postFile,exception is %@",http_tag,ex.reason);
        callback(NULL);
    }
    @finally {
        
    }
    
    
    
}



-(BOOL)downLoadFile:(NSString *)urlStr  filePath:(NSString *)filePath
{
    NSLog(@"%s,downLoadFile entered, the url is %@, filePath is %@.",http_tag,urlStr,filePath);
    if(urlStr==NULL||[urlStr length]<=0||filePath==NULL)
    {
        NSLog(@"%s,downLoadFile,parameters is illegal",http_tag);
        return NO;
    }
    NSURL *url=[NSURL URLWithString:urlStr];
    if(url==NULL)
    {
        NSLog(@"%s,downLoadFile,string to url failed",http_tag);
        return NO;
    }
    @try {
        NSData *data=[NSData dataWithContentsOfURL:url];
        if(data==NULL)
        {
            NSLog(@"%s,downLoadFile,data is null.",http_tag);
            return NO;
        }
        BOOL result=[data writeToFile:filePath atomically:YES];
        if(result)
        {
            NSLog(@"%s,downLoadFile,save file success.",http_tag);
        }
        else
        {
            NSLog(@"%s,downLoadFile,save file fail.",http_tag);
        }
        return result;

    }
    @catch (NSException *exception) {
        
        NSLog(@"%s,downLoadFile,exception is %@.",http_tag,[exception reason]);
    }

    
}
/*
-(void)fetchNewRefreshToken:(ReRequester *)reRequester
{
    NSLog(@"%s,fetchNewRefreshToken entered.",http_tag);
    if(reRequester==NULL||reRequester.waitingCallBack==NULL)
    {
        NSLog(@"%s,fetchNewRefreshToken,reRequester is null",http_tag);
        return;
    }
    SyncResult valid=[self checkNetwork];
    if(valid!=SUCESS)
    {
        reRequester.waitingCallBack(NULL);
        return ;
    }
    NSString *urtStr=[NSString stringWithFormat:@"%@://%@/v1/auth/requestRefreshToken.json",protol,hostName];
    User *user=[User sharedSingleton];
    NSString *uid=user.uid;
    NSString *ts=[Tooler getTS];
    NSString *pass=user.password;
    NSString *phoneNum=user.account;
    NSString *sign=[Tooler hashByHmac:pass data:[[[NSString stringWithFormat:@"%@",uid] stringByAppendingString:phoneNum] stringByAppendingString:ts]];
    NSMutableDictionary *param=[[NSMutableDictionary alloc] init];
    [param setValue:uid forKey:@"uid"];
    [param setValue:phoneNum forKey:@"phoneNum"];
    [param setValue:pass forKey:@"password"];
    [param setValue:phoneNum forKey:@"fullName"];
    [param setValue:ts forKey:@"ts"];
    [param setValue:sign forKey:@"sign"];
    
    [self asyncPost:urtStr parameters:param completion:^(NSDictionary *resdict) {
        if(resdict==NULL)
        {
            NSLog(@"%s,fetchNewRefreshToken,redict is null.",http_tag);
            reRequester.waitingCallBack(NULL);
            return ;
        }
        NSInteger sucess=[[resdict valueForKey:@"success"] integerValue];
        if(sucess!=1)
        {
            NSString *errorCode=[resdict valueForKey:@"errorCode"];
            NSLog(@"%s,fetchNewRefreshToken,error code is %@",http_tag,errorCode);
            reRequester.waitingCallBack(NULL);
            return ;
        }
        NSString *token=[resdict valueForKey:@"accessToken"];
        NSLog(@"%s,fetchNewRefreshToken,the new refresh token is %@",http_tag,token);
        [self fetchNewAccessToken:reRequester refreshToken:token];
    }];
}
*/
-(void)fetchNewAccessToken:(ReRequester *)reRequester
{
    NSLog(@"%s,fetchNewAccessToken entered.",http_tag);
    if(reRequester==NULL||reRequester.url==NULL||reRequester.waitingCallBack==NULL)
    {
        NSLog(@"%s,fetchNewAccessToken,reRequester is illegal.",http_tag);
        return;
    }
    SyncResult valid=[self checkNetwork];
    if(valid!=SUCESS)
    {
        reRequester.waitingCallBack(NULL);
        return ;
    }
    NSString *urtStr=[NSString stringWithFormat:@"%@://%@/v1/auth/requestAccessToken.json",protol,hostName];
    User *user=[User sharedSingleton];
    NSString *refreshToken=user.refreshToken;
    if(refreshToken==NULL)
    {
        NSLog(@"%s,fetchNewAccessToken,refreshToken is null.",http_tag);
        reRequester.waitingCallBack(NULL);
        return;
    }
    NSString *uid=user.uid;
    NSString *ts=[Tooler getTS];

    if(uid==NULL||!([uid integerValue]>0))
    {
        NSLog(@"%s,fetchNewAccessToken,uid is null",http_tag);
        reRequester.waitingCallBack(NULL);
        return;
    }
    NSMutableDictionary *param=[[NSMutableDictionary alloc] init];
    [param setValue:uid forKey:@"uid"];
    [param setValue:ts forKey:@"ts"];
    [param setValue:refreshToken forKey:@"refreshToken"];
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate!=NULL&&mydelegate.iosToken!=NULL)
        [param setValue:mydelegate.iosToken forKey:@"token"];
    [self asyncPost:urtStr parameters:param completion:^(NSDictionary *resdict) {
        if(resdict==NULL||[resdict count]==0)
        {
            NSLog(@"%s,fetchNewAccessToken,resdict is null",http_tag);
            reRequester.waitingCallBack(NULL);
            return;
        }
        NSInteger ret=[[resdict valueForKey:@"success"] integerValue];
        if(ret==1)
        {
            NSString *accessToken=[resdict valueForKey:@"token"];
            NSLog(@"%s,fetchNewAccessToken,success,access token is %@",http_tag,accessToken);
            if(accessToken==NULL)
            {
                NSLog(@"%s,fetchNewAccessToken,fail,access token is null",http_tag);
                reRequester.waitingCallBack(NULL);
                return;
            }
            user.accessToken=accessToken;
            user.refreshToken=refreshToken;
            if(self.theManager!=NULL)
            {
                [self.theManager updateUser:user];
            }
            //继续请求之前的API
            if(reRequester!=NULL)
                [self performWaitingRequestRequest:reRequester];
        }
        else if(ret==0)
        {
            NSString *errorCode=[resdict valueForKey:@"errorCode"];
            NSLog(@"%s,fetchNewAccessToken,error code is %@",http_tag,errorCode);
            if(errorCode!=NULL&&[errorCode intValue]==USER_LOGINED_ON_OTHER_DEVICE)
            {
                reRequester.waitingCallBack(resdict);
                [[NSNotificationCenter defaultCenter] postNotificationName:USERALREADYLOGIN object:nil  userInfo:nil];
            }
            else if(errorCode!=NULL&&[errorCode intValue]==API_REFRESHTOKEN_INVALID)
            {
                reRequester.waitingCallBack(resdict);
                [[NSNotificationCenter defaultCenter] postNotificationName:LONGTIMENOLOGIN object:nil  userInfo:nil];
            }
            else
            {
                reRequester.waitingCallBack(NULL);
            }

        }
        else
        {
            NSLog(@"%s,fetchNewAccessToken,program error",http_tag);
            reRequester.waitingCallBack(NULL);
        }
    }];


    
}



-(void)performWaitingRequestRequest:(ReRequester *)reRequester
{
    NSLog(@"%s,performWaitingRequestRequest entered.",http_tag);
    if(reRequester==NULL||reRequester.url==NULL||reRequester.waitingCallBack==NULL)
    {
        NSLog(@"%s,performWaitingRequestRequest,reRequester is null.",http_tag);
        return;
    }
    SyncResult valid=[self checkNetwork];
    if(valid!=SUCESS)
    {
        reRequester.waitingCallBack(NULL);
        return;
    }
    switch (reRequester.methord) {
        case GET:
            
            break;
        case POST:
            break;
        case ASYNCGET:
            break;
        case ASYNCPOST:
        {
            User *user=[User sharedSingleton];
            [reRequester.postPara setValue:user.accessToken forKey:@"atoken"];
            [self asyncPost:reRequester.url parameters:reRequester.postPara completion:^(NSDictionary *resdict) {

                if(resdict==NULL||[resdict count]==0)
                {
                    NSLog(@"%s,performWaitingRequestRequest,resdict is null",http_tag);
                    reRequester.waitingCallBack(NULL);
                    return;
                }
                
                NSInteger ret=[[resdict valueForKey:@"success"] integerValue];
                if(ret==1)
                {
                    reRequester.waitingCallBack(resdict);
                }
            }];

            break;
        }
        case POSTFILE:
        {
            User *user=[User sharedSingleton];
            [reRequester.postPara setValue:user.accessToken forKey:@"atoken"];
            [self postFile:reRequester.url fileName:reRequester.fileName filePath:reRequester.filePath param:reRequester.postPara completion:^(NSDictionary *resdict) {
                if(resdict==NULL||[resdict count]==0)
                {
                    NSLog(@"%s,performWaitingRequestRequest,resdict is null",http_tag);
                    reRequester.waitingCallBack(NULL);
                    return;
                }
                
                NSInteger ret=[[resdict valueForKey:@"success"] integerValue];
                if(ret==1)
                {
                    reRequester.waitingCallBack(resdict);
                }
            }];
            
            break;
        }
    }
}
-(SyncResult)checkNetwork
{
    //判断当前网络状况
    if([Tooler checkNetworkState]==3)
    {
        return NO_NET_WORK;
    }
    
    return SUCESS;
}

@end
